<template>
  <view>
    <view class="cu-list menu">
      <view class="cu-item arrow" @click="gourl('phone')">
        <view class="content" style="font-size: 16px">
          <text class="cuIcon-shake text-red"></text>
          <text class="">{{ user.mobile }}</text>
        </view>
      </view>
      <view class="cu-item arrow" @click="gourl('safe')">
        <view class="content" style="font-size: 16px">
          <text class="cuIcon-safe text-green"></text>
          <text class="">安全中心</text>
        </view>
      </view>
      <!-- #ifdef MP-WEIXIN -->
      <view class="cu-item arrow" @click="iSshow = true">
        <view class="content" style="font-size: 16px; display: flex">
          <text class="cuIcon-safe text-green"></text>
          <view class="weix">
            <view class="">微信绑定</view>
            <view style="color: #e54d42" v-if="isBindWechat == true">已绑定</view>
            <view v-if="isBindWechat == false">未绑定</view>
          </view>
        </view>
      </view>
      <!-- #endif -->
      <!-- #ifdef APP -->
      <view class="cu-item arrow" @click="appisShow = true">
        <view class="content" style="font-size: 16px; display: flex">
          <text class="cuIcon-safe text-green"></text>
          <view class="weix">
            <view class="">微信绑定</view>
            <view style="color: #e54d42" v-if="appisBindWechat == true">已绑定</view>
            <view v-if="appisBindWechat == false">未绑定</view>
          </view>
        </view>
      </view>
      <!-- #endif -->
      <view class="cu-item arrow" @click="gourl('email')">
        <view class="content" style="font-size: 16px">
          <text class="cuIcon-mail text-cyan"></text>
          <text class="" v-if="user.email != null && user.email != ''">{{ user.email }}</text>
          <text class="text-gray" v-else>请设置邮箱</text>
        </view>
      </view>
      <view class="cu-item arrow" @click="gourl('../userinfo/userbank')">
        <view class="content" style="font-size: 16px">
          <text class="cuIcon-recharge text-pink"></text>
          <text class="">提现账户管理</text>
        </view>
      </view>
    </view>
    <view class="cu-list menu">
      <view class="cu-item arrow" @click="gourl('../index/agree?id=18')">
        <view class="content" style="font-size: 16px">
          <text class="cuIcon-edit text-purple"></text>
          <text class="">用户协议</text>
        </view>
      </view>
      <view class="cu-item arrow" @click="gourl('../index/agree?id=19')">
        <view class="content" style="font-size: 16px">
          <text class="cuIcon-attentionfavorfill text-cyans"></text>
          <text class="">隐私协议</text>
        </view>
      </view>
    </view>
    <view class="cu-list menu">
      <view class="cu-item text-center">
        <view class="content solid-bottom padding" style="font-size: 16px" @tap="logout">
          <text class="text-red">退出登陆</text>
        </view>
      </view>
    </view>
    <!-- 小程序弹层 -->
    <u-modal :show="iSshow" cancelText="取消" confirmText="确定" title="微信" :showCancelButton="true" @cancel="iSshow = false" @confirm="handlerConfirm">
      <view class="">
        <view v-if="isBindWechat == false" class="flex flex-center padding-20-0">当前账号未绑定微信，是否绑定微信账号</view>
        <view v-if="isBindWechat == true" class="flex flex-center padding-20-0">当前账号已绑定微信，是否解绑微信账号</view>
      </view>
    </u-modal>
    <!-- app弹层 -->
    <u-modal :show="appisShow" cancelText="取消" confirmText="确定" title="微信" :showCancelButton="true" @cancel="appisShow = false" @confirm="handler">
      <view class="">
        <view v-if="appisBindWechat == false" class="flex flex-center padding-20-0">当前账号未绑定微信，是否绑定微信账号</view>
        <view v-if="appisBindWechat == true" class="flex flex-center padding-20-0">当前账号已绑定微信，是否解绑微信账号</view>
      </view>
    </u-modal>
  </view>
</template>
<script>
export default {
  data() {
    return {
      user: [],
      iSshow: false, //控制小程序弹出层
      appisShow: false, //app弹出层
      // bd: 1, //0绑定 1未绑定
      isBindWechat: '', //小程序
      appisBindWechat: ''
    };
  },
  onLoad() {
    var sessionId = uni.getStorageSync('sessionId');
    // if (sessionId) {
    //   this.bd = 0;
    //   console.log(this.bd);
    // } else {
    //   this.bd = 1;
    //   console.log(this.bd);
    // }
    let gv = JSON.parse(uni.getStorageSync('users'));
    this.user = gv;
    // #ifdef MP-WEIXIN
    this.getUsets();
    // #endif
    // #ifdef APP
    this.appgetUsets();
    // #endif
  },
  methods: {
    // app绑定微信
    handler() {
      var that = this;
      if (this.appisBindWechat == false) {
        //客户端成功获取授权临时票据（code）,向业务服务器发起登录请求。
        uni.login({
          provider: 'weixin',
          onlyAuthorize: true, // 微信登录仅请求授权认证
          success: async function (event) {
            const { code } = event;
            console.log(code);

            uni.request({
              url: that.app.apiHost + '/bindWechat',
              header: { Authorization: 'bearer ' + that.app.getAuthCode(), platform: 'openPlatform' },
              method: 'POST',
              data: {
                payload: JSON.stringify({ code: code })
              },
              success: (res) => {
                uni.hideLoading();
                if (res.data.code == 0) {
                  that.app.showMsg(res.data.msg);
                } else {
                  that.app.showMsg(res.data.msg);
                }
                console.log(7777, res);
                that.appisShow = false;
                that.appgetUsets();
              },
              fail(err) {
                uni.hideLoading();
                that.app.showMsg('绑定失败');
              }
            });
          },
          fail() {
            that.app.showMsg('绑定失败');
          }
        });
      } else {
        uni.request({
          url: that.app.apiHost + '/unbindWechat',
          method: 'POST',
          header: { Authorization: 'bearer ' + that.app.getAuthCode(), platform: 'openPlatform' },
          success: (res) => {
            console.log(res);
            that.app.showMsg(res.data.msg);
            this.appisShow = false;
            this.appgetUsets();
          },
          fail(res) {}
        });
        console.log('解除绑定');
      }
    },
    //小程序绑定微信
    handlerConfirm() {
      var sessionId = uni.getStorageSync('sessionId');
      var that = this;
      if (this.isBindWechat == false) {
        //客户端成功获取授权临时票据（code）,向业务服务器发起登录请求。
        uni.login({
          provider: 'weixin',
          onlyAuthorize: true, // 微信登录仅请求授权认证
          success: async function (event) {
            const { code } = event;

            var sessionId = uni.getStorageSync('sessionId');
            uni.request({
              url: that.app.apiHost + '/getSessionId',
              header: { Authorization: '', platform: 'miniProgram' },
              method: 'POST',
              data: {
                payload: encodeURIComponent(JSON.stringify({ code: code }))
              },
              success: (res) => {
                uni.setStorageSync('sessionId', res.data.data.session_id);
                uni.request({
                  url: that.app.apiHost + '/bindWechat',
                  method: 'POST',
                  data: {
                    payload: encodeURIComponent(JSON.stringify({ sessionId: res.data.data.session_id }))
                  },
                  header: { Authorization: 'bearer ' + that.app.getAuthCode(), platform: 'miniProgram' },
                  success: (ree) => {
                    uni.hideLoading();
                    if (ree.data.code == 0) {
                      that.app.showMsg(ree.data.msg);
                    } else {
                      that.app.showMsg(ree.data.msg);
                    }
                    console.log(7777, ree);
                    that.iSshow = false;
                    that.getUsets();
                  },
                  fail(err) {
                    console.log(2222, err);
                  }
                });
              },
              fail(err) {
                uni.hideLoading();
                that.app.showMsg('绑定失败');
              }
            });
          },
          fail() {
            that.app.showMsg('绑定失败');
          }
        });
        console.log(2222);
      } else {
        uni.request({
          url: that.app.apiHost + '/unbindWechat',
          method: 'POST',
          header: { Authorization: 'bearer ' + that.app.getAuthCode(), platform: 'miniProgram' },
          success: (res) => {
            console.log(res);
            that.app.showMsg(res.data.msg);
            this.iSshow = false;
            this.getUsets();
          },
          fail(res) {}
        });
        console.log('解除绑定');
      }
    },
    gourl(url) {
      uni.navigateTo({
        url: url
      });
    },
    logout() {
      uni.showLoading({
        mask: true
      });
      var that = this;
      uni.request({
        url: that.app.apiHost + '/logout',
        method: 'POST',
        header: { Authorization: 'bearer ' + that.app.getAuthCode() },
        success: (res) => {
          console.log(res.data);
          if (res.data.code == 3) {
            uni.hideLoading();
            that.app.showMsg('退出成功!', '../login/login');
            // that.app.showMsg('退出成功!', '../index/index');
            uni.clearStorageSync();
            setTimeout(
              uni.switchTab({
                url: '/pages/login/login'
              }),
              2000
            );
          }
        },
        fail(res) {}
      });
    },
    //app获取个人信息
    appgetUsets() {
      var that = this;
      uni.request({
        url: that.app.apiHost + '/mindex',
        method: 'POST',
        header: { Authorization: 'bearer ' + that.app.getAuthCode(), platform: 'openPlatform' },
        success: (res) => {
          this.appisBindWechat = res.data.data.isBindWechat;
          console.log(77777, res.data.data);
        },
        fail(res) {
          console.log(res);
        }
      });
    },

    // 小程序获取个人信息
    getUsets() {
      var that = this;
      uni.request({
        url: that.app.apiHost + '/mindex',
        method: 'POST',
        header: { Authorization: 'bearer ' + that.app.getAuthCode(), platform: 'miniProgram' },
        success: (res) => {
          this.isBindWechat = res.data.data.isBindWechat;
          console.log(8888, res.data.data);
        },
        fail(res) {
          console.log(res);
        }
      });
    }
  }
};
</script>

<style>
.weix {
  width: 590rpx;
  display: flex;
  justify-content: space-between;
}
page {
  background-color: #f1f1f1;
}
</style>
